// Problem 040: Champernowne's constant
// An irrational decimal fraction is created by concatenating the positive integers:
// 0.123456789101112131415161718192021...
// It can be seen that the 12th digit of the fractional part is 1.
// If dn represents the nth digit of the fractional part, find the value of the following expression.
// d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000

package main

import (
	"fmt"
	"strconv"
)

func p040() {
	ans := count040(10) * count040(100) * count040(1000) * count040(10000) * count040(100000) * count040(1000000)
	fmt.Println("Problem 040:", ans)
}

func count040(order int) int {
	w, lw, n := 0, 0, 1
	for n = 1; lw < order; n++ {
		x := n
		for x%10 == 0 {
			x /= 10
		}
		if x == 1 {
			w++
		}
		lw += w

	}
	lw, n = lw-w, n-1
	o := order - lw
	x := strconv.Itoa(n)[o-1 : o]
	a, _ := strconv.Atoi(x)
	return a
}
